package com.lwl.blue.commont.utils;

import com.lwl.blue.entity.Users;
import com.lwl.blue.vo.LoginUserInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static com.lwl.blue.commont.base.Const.LONGIN_ATTR;

/**
 *
 * @Descr 用户上下文对象：设置和获取HttpSession登录的用户
 */
public class UserContextUtil {

    public static void setUser(LoginUserInfo users, HttpSession session) {
        session.setAttribute(LONGIN_ATTR, users);
    }

    public static LoginUserInfo getUser(HttpSession session) {
        return (LoginUserInfo)session.getAttribute(LONGIN_ATTR);
    }

    public static LoginUserInfo getUser() {
        return (LoginUserInfo)getsession().getAttribute(LONGIN_ATTR);
    }

    public static HttpSession getsession() {
        return getRequest().getSession();
    }

    public static HttpServletRequest getRequest()
    {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = requestAttributes.getRequest();

        return request;
    }
}
